#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include"path.h"//路径配置
/**
  *soj服务开启和命令控制程序
  *soj start 开启服务 soj stop停止服务站soj destroy终止服务
  *soj -L c -F xx.c -P 1001 -U xxs -T -TL 1500 -ML 10000 -R 10000检测代码程序
*/

int main(int args,char *argc[])
{
     int status;
     FILE *s;
     FILE *c;
     FILE *cl;
     char status_path[100];//服务状态文件位置
     strcpy(status_path,path);
     strcat(status_path,"command/status");

     char command_path[100];//服务控制命令文件存放路径
     strcpy( command_path,path);
     strcat( command_path,"command/command"); 


   
     
     //获取服务状态0为没有开启，1为开启了

  
     if((s=fopen(status_path,"r"))==NULL){
       status=0;
     }
     if(!feof(s)){
       fscanf(s,"%d",&status);
          if(status!=1)
            status=0;
     }
     else{
           status=0;
     }
     fclose(s);
    //////////////////////////////////////////////////////
     if(status==0){//服务未开启
                
		           if(strcmp(argc[1],"start")==0){//如果调用命令为./soj start启动服务        
       	                 if((c=fopen(command_path,"w"))==NULL){
                                          printf("{'msg':'system error'}");
      			               }
     		                   fprintf(c,"start");       
                               fclose(c);
                                         //开启服务
                               pid_t pid;
                               pid =vfork();
                               if(pid<0) { 
                                    printf("{'msg':'start failed'}");
                                    return -1;
                                         }
                               else if(pid==0){
                                  printf("{'msg':'severce start'}");
                                  char severpath[100];
                                  strcpy(severpath,path);
                                       strcat(severpath,"mainback");
                                  execlp(severpath,NULL);//启动服务程序                           
                           	         }                            
                              if((s=fopen(status_path,"w"))==NULL){
                                   printf("{'msg':'system error'}");
               		               }
           			      fprintf(s,"1");//写服务已启动
                              fclose(s);    
                              return 3;//服务开启
       		        }
      		else{
            	     printf("{'msg':'severce not start'}");
                       return -2;//服务未启动
       	                }
      }
/////////////////////////////////////////////////////////////////////////////////////////
     else{   //服务已开启
          
        if(strcmp(argc[1],"start")==0){//如果调用命令为./soj start继续服务        
           if((c=fopen(command_path,"w"))==NULL){
      	   }
           fprintf(c,"start"); 
           fclose(c); 
           printf("{'msg':'service continue'}\n");
           return 1;//服务继续     
        
       	}
     	else if(strcmp(argc[1],"stop")==0){ //如果调用命令为./soj start继续服务
             
           if((c=fopen(command_path,"w"))==NULL){
                printf("{'msg':'system error'}");
      	       }
           fprintf(c,"stop");   
           fclose(c); 
           printf("{'msg':'severce stop'}\n");
           return 2;//服务停止   
  
     	}
    	else if(strcmp(argc[1],"destroy")==0){ //如果调用命令为./soj destroy  终止服务            
           if((c=fopen(command_path,"w"))==NULL){
                 printf("{'msg':'system error'}");
      	       }
           fprintf(c,"destroy");
           fclose(c);    
           if((s=fopen(status_path,"w"))==NULL){
               }
           fprintf(s,"0");
           fclose(s);        
           printf("{'msg':'severce destroy'}\n");
           return 3;//服务终止
      
     	}
      else if(strcmp(argc[1],"-L")==0){
                  char checktask_path[100];//检测列表文件存放路径
     			strcpy( checktask_path,path);
     			strcat( checktask_path,"checktask/checktask");
                time_t   timep;  
     			long tss=time(&timep);//获取当前时间
     			char ts[20];
     			 int i;
     			 for(i=0;tss>0;i++){
     			 	ts[i]=tss%10+'0';
     			 	tss/=10;
     			 }
     			 ts[i]='\0';
     			strcat(checktask_path,ts);//当前时间与文件名链接确保唯一性
    
                 if((cl=fopen(checktask_path,"w"))==NULL){
                          printf("{'msg':'system error'}");
      	           }
                     for(i=1;i<=15;i++){
                        fprintf(cl,"%s ",argc[i]);
                            }
                     fclose(cl);                   
                    
                   }
      else{
                printf("{'msg':'no this command'}");
                return 4;//无此命令
        }
     }
}
